<!--
 * @Description: 字典类型
 * @Author: Liu Zhiwang
 * @Date: 2021-04-09 11:49:17
 * @LastEditTime: 2021-06-08 19:17:00
 * @LastEditors: Liu Zhiwang
-->
<template>
  <div class="app-container v-flex">
    <filter-form :form="listQuery" :data="filterData" only-query @search="search" @addForm="addForm">
      <el-button
        class="refresh-btn"
        size="mini"
        type="primary"
        icon="el-icon-refresh"
        @click="resetListQuery">重置</el-button>
      <el-button
        v-permission-button="publicConfig['ITPS-ADD']"
        size="mini"
        class="operation-btn"
        type="primary"
        icon="el-icon-plus"
        @click="addForm">新增</el-button>
    </filter-form>

    <!-- 表格 -->
    <div ref="main" class="v-flex-elastic">
      <base-table
        :height="height"
        :loading="tableData.loading"
        :data="tableData.tableList"
        :columns="tableData.columns"
        :show-index="tableData.showIndex"
        :show-selection="tableData.showSelection"
        :show-operation="tableData.showOperation"
        :show-filter-column="tableData.showFilterColumn"
        :operaton-width="tableData.operatonWidth"
        :default-operations="tableData.defaultOperations"
        :has-auth="tableData.hasAuth"
        :row-show-color="tableData.rowShowColor"
        @view="view"
        @updateForm="updateForm"
        @deleteData="deleteData">
        <template slot="isSystem" slot-scope="{row, $index}">
          <el-switch v-model="row.isSystem" :active-value="1" :inactive-value="0" @change="enable(row, $index)" />
        </template>
        <template slot="isPub" slot-scope="{row, $index}">
          <el-switch v-model="row.isPub" :active-value="1" :inactive-value="0" @change="enable(row, $index)" />
        </template>
        <template slot="status" slot-scope="{row, $index}">
          <el-switch v-model="row.status" :active-value="1" :inactive-value="0" @change="enable(row, $index)" />
        </template>
      </base-table>
    </div>
    <!-- 表格 -->

    <!-- 分页 -->
    <div class="page-box">
      <pagination
        :total="tableData.total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="changePageOption" />
    </div>
    <!-- 分页 -->

    <!-- 新增或编辑弹框 -->
    <DataAddOrEdit :dialog="dialog" @updateData="updateData" />

    <!-- 查看弹框 -->
    <DataLook :dialog="dialog" :dialog-look="dialogLook" />
  </div>
</template>
<script>
// import { getListPage, addOrEditData, deleteData } from '@/api/sysm/dictionary/type'
import { tableMixin } from '@/utils/public'
import DataAddOrEdit from './DataAddOrEdit'
import DataLook from './DataLook'

export default {
  name: 'Name',
  components: {
    DataAddOrEdit,
    DataLook
  },
  mixins: [tableMixin],
  data() {
    return {
      // 表格高度
      height: 0,

      // 顶部操作栏配置
      filterData: {
        keyName: {
          key: 'keyName',
          type: 'input',
          placeholder: '请输入名称、编号、描述'
        }
      },

      // 查询列表请求参数
      listQuery: {
        page: 1,
        size: 20
      },

      // 表格数组
      tableData: {
        loading: false,
        tableList: [],
        total: 0,
        columns: [
          { prop: 'dtName', label: '类型名称' },
          { prop: 'dtCode', label: '类型编号' },
          { prop: 'dtDec', label: '描述' },
          { prop: 'isSystem', label: '是否系统', template: true, width: 80 },
          { prop: 'isPub', label: '是否公用', template: true, width: 80 },
          { prop: 'status', label: '启用', template: true, width: 80 }
        ],
        showIndex: true,
        showSelection: false,
        showOperation: true,
        showFilterColumn: true,
        operatonWidth: '160px',
        defaultOperations: ['view', 'edit', 'delete'],
        hasAuth: true,
        rowShowColor: { name: 'name', value: 0 }
      },

      // 添加/编辑弹框
      dialog: {
        title: '',
        visible: false,
        disabled: false,
        loading: false,
        dataList: {
          lineList: []
        },
        form: {},
        rules: {
          dtName: [{ required: true, message: '必填', trigger: 'blur' },
            { min: 1, message: '大于1个字符', trigger: 'blur' },
            { max: 20, message: '小于20个字符', trigger: 'blur' }],
          dtCode: [{ required: true, message: '必填', trigger: 'blur' },
            { min: 1, message: '大于1个字符', trigger: 'blur' },
            { max: 20, message: '小于20个字符', trigger: 'blur' }],
          dtDec: [{ max: 255, message: '小于255个字符', trigger: 'blur' }]
        }
      },

      // 查看弹框
      dialogLook: {
        visible: false
      }
    }
  },
  activated() {
    this.getListPage()
    this.getList()
  },
  mounted() {},
  methods: {
    // 获取下拉数据、字典
    getList() {
    },

    // 查询新闻信息列表
    getListPage() {
      this.tableData.loading = true
      getListPage(this.listQuery).then(response => {
        this.tableData.tableList = response.data.records
        this.tableData.total = response.data.total
        this.tableData.loading = false
      }).catch(() => {
        this.tableData.loading = false
      })
    },

    // 新增
    addForm() {
      const dialog = {
        title: '添加',
        type: 'add',
        visible: true,
        disabled: false,
        loading: false,
        dataList: {
          lineList: this.dialog.dataList.lineList
        },
        form: {
          isSystem: 0,
          isPub: 0,
          status: 1,
          delFlag: 0
        }
      }
      this.dialog = Object.assign(this.dialog, dialog)
    },

    // 编辑
    updateForm(row, index) {
      this.currentRow = [row]
      const dialog = {
        title: '编辑',
        type: 'edit',
        visible: true,
        disabled: false,
        loading: false,
        dataList: {
          lineList: this.dialog.dataList.lineList
        },
        form: JSON.parse(JSON.stringify(row))
      }
      this.dialog = Object.assign(this.dialog, dialog)
    },

    view(row, index) { // 查看
      this.currentRow = [row]
      const dialog = {
        title: '查看',
        type: 'look',
        visible: false,
        disabled: false,
        loading: false,
        form: JSON.parse(JSON.stringify(row))
      }
      this.dialog = Object.assign(this.dialog, dialog)
      this.dialogLook.visible = true
    },

    // 查询
    search() {
      this.listQuery.page = 1
      this.getListPage()
    },

    // 新增或编辑弹框$emit 回调方法
    updateData(formData) {
      const param = Object.assign({}, formData)
      addOrEditData(param)
        .then((response) => {
          this.currentRow = [response.data]
          if (this.dialog.type === 'add') {
            this.tableData.tableList.unshift(this.currentRow[0])
            this.tableData.total++
            this.notifiySuccess('添加成功')
            var dialog = this.dialog
            dialog.form.id = response.data.id
            this.dialog = Object.assign(this.dialog, dialog)
          } else {
            const index = this.tableData.tableList.findIndex(
              (item) => item.id === this.currentRow[0].id
            )
            this.tableData.tableList.splice(
              index,
              1,
              JSON.parse(JSON.stringify(this.currentRow[0]))
            )
            this.$forceUpdate()
            this.notifiySuccess('保存成功')
          }
          this.dialog.loading = false
          this.dialog.visible = false
        })
        .catch((_) => {
          this.dialog.loading = false
        })
    },

    // 启用、停用,是否系统
    enable(row, $index) {
      this.rowIndex = $index
      this.updateData(row, 2)
    },

    // 删除
    deleteData(row, index) {
      this.confirmBox(`确定删除这条数据?`)
        .then(() => {
          deleteData(row.id).then(response => {
            this.notifiySuccess('删除成功!')
            this.getListPage()
          })
        })
        .catch(() => {})
    },

    // 改变分页配置
    changePageOption(data) {
      this.listQuery.page = data.page
      this.listQuery.size = data.size
      this.getListPage()
    },

    // 重置查询
    resetListQuery() {
      this.listQuery = {
        page: 1,
        size: 20
      }
      this.getListPage()
    }
  }
}
</script>
